//
//  HNCourseEvaluationDetailVC.m
//  ZYManagementSystem
//
//  Created by 星道 on 17/6/19.
//  Copyright © 2017年 焕宁. All rights reserved.
//

#import "HNCourseEvaluationDetailVC.h"
#import "HNCreditGoldSelectView.h"

@interface HNCourseEvaluationDetailVC ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
    //xib面板控件
    //学生账号
    IBOutlet UILabel *_StudentAccount;
    //科目
    IBOutlet UILabel *_Subject;
    //作业图片
    IBOutlet UIImageView *_Task;
    //共性问题
    IBOutlet UITextField *_Character;
    //共性图片
    IBOutlet UIImageView *_CharacterImg;
    //个性问题
    IBOutlet UITextField *_Personality;
    //个性图片
    IBOutlet UIImageView *_PersonalityImg;
}

/**
 作业图片、共性图片、个性图片的临时存放地
 */
@property (nonatomic, weak) UIImageView *TempImgView;
@end

@implementation HNCourseEvaluationDetailVC


- (void)viewDidLoad {

    [super viewDidLoad];

    //学生账号赋值
    _StudentAccount.text = self.Account;

    //设置图片手势
    self.SetupImgView();
}







#pragma mark - 网络请求
/**
 开始上传图片
 */
- (instancetype (^)(UIImage *))UpdataImg {

    return ^(UIImage *Img) {

        [MBProgressHUD showHUD];

//        [MTHttpTools UpdataImgWithURL:LLBaseFileUrl(@"upload.do") pic:Img parameter:@{@"action":@"uploadimage"} success:^(id obj) {
//            //上传成功后显示图片
//            dispatch_async(dispatch_get_main_queue(), ^{
//                self.MgrGetImg(Img);
//            });
//        } fail:^(NSError *error) {
//            //上传失败
//            [MBProgressHUD showError:LLNetErrStr];
//        }];
//
        return self;
    };
}








#pragma mark - 点击事件
/**
 单击手势 作业1100 共性1101 个性1102 图片事件
 */
- (void)ClickImgView:(UITapGestureRecognizer *)sender {

    UIImageView *ImgView = (UIImageView *)[self.view viewWithTag:sender.view.tag];
    self.MgrTaskImg(ImgView);
}

/**
 单击了选择科目
 */
- (IBAction)ClickSubject:(UIButton *)sender {

    self.MgrSelectSubject();
}








#pragma mark - 处理
/**
 设置图片的手势
 */
- (instancetype (^)())SetupImgView {

    return ^() {

        //给三张图片按钮添加单击手势，tag值：作业1100 共性1101 个性1102
        UITapGestureRecognizer *Tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ClickImgView:)];
        UITapGestureRecognizer *Tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ClickImgView:)];
        UITapGestureRecognizer *Tap3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ClickImgView:)];
        //赋值对象
        [_Task addGestureRecognizer:Tap];
        [_CharacterImg addGestureRecognizer:Tap2];
        [_PersonalityImg addGestureRecognizer:Tap3];

        //给三张图片按钮添加长按手势，tag值：作业1100 共性1101 个性1102
        UILongPressGestureRecognizer *LongPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(MgrDeleteButtonImg:)];
        UILongPressGestureRecognizer *LongPress2 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(MgrDeleteButtonImg:)];
        UILongPressGestureRecognizer *LongPress3 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(MgrDeleteButtonImg:)];
        //设置长按时长
        LongPress.delaysTouchesBegan = 0.8;
        //赋值对象
        [_Task addGestureRecognizer:LongPress];
        [_CharacterImg addGestureRecognizer:LongPress2];
        [_PersonalityImg addGestureRecognizer:LongPress3];
        return self;
    };
}


/**
 处理 作业1100 共性1101 个性1102 图片的上传
 */
- (instancetype (^)(UIImageView *))MgrTaskImg {

    return ^(UIImageView *TaskImg) {

        //打开相册选择器
        self.TempImgView = TaskImg;
        self.OpenImgPickView();
        return self;
    };
}



/**
 处理代理回来的图片
 */
- (instancetype (^)(UIImage *))MgrGetImg {

    return ^(UIImage *Img) {

        //显示出图片1
        self.TempImgView.image = Img;
        return self;
    };
}


/**
 处理选择科目
 */
- (instancetype (^)())MgrSelectSubject {

    return ^() {

        /**
         打开选择器
         */
        HNCreditGoldSelectView *SelectView = [[HNCreditGoldSelectView alloc] initWithTitle_Arr:@[@"语文",@"数学",@"英语"] SelectRow:^(NSString *Title) {

            //赋值
            _Subject.text = Title;
        }];
        [[UIApplication sharedApplication].keyWindow addSubview:SelectView];
        return self;
    };
}


/**
 用来处理长按删除图片的功能
 */
- (void)MgrDeleteButtonImg:(UILongPressGestureRecognizer *)Long {

    //弹框询问是否删除
    UIAlertController *Alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"是否删除?" preferredStyle:UIAlertControllerStyleActionSheet];
    [Alert addAction:[UIAlertAction actionWithTitle:@"删除" style:(UIAlertActionStyleDestructive) handler:^(UIAlertAction * _Nonnull action) {
        //开始删除
        UIButton *Button = (UIButton *)[self.view viewWithTag:Long.view.tag];
        [Button setBackgroundImage:[UIImage imageNamed:@"btn_add_sel"] forState:(UIControlStateNormal)];
        [Button setBackgroundImage:[UIImage imageNamed:@"btn_add_sel"] forState:(UIControlStateHighlighted)];
    }]];
    [Alert addAction:[UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil]];
    [self presentViewController:Alert animated:YES completion:nil];
}



#pragma mark - 重用方法
/**
 打开相册
 */
- (instancetype (^)())OpenImgPickView {

    return  ^() {

        //调用系统相册的类
        UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
        //设置选取的照片是否可编辑
        pickerController.allowsEditing = NO;
        //设置相册呈现的样式
        pickerController.sourceType =  UIImagePickerControllerSourceTypePhotoLibrary;//直接全部呈现系统相册
        //照片的选取样式还有以下两种
        //UIImagePickerControllerSourceTypePhotoLibrary,直接全部呈现系统相册
        //UIImagePickerControllerSourceTypeSavedPhotosAlbum;//图片分组列表样式
        //选择完成图片或者点击取消按钮都是通过代理来操作我们所需要的逻辑过程
        pickerController.delegate = self;
        //使用模态呈现相册
        [self presentViewController:pickerController animated:YES completion:nil];
        return self;
    };
}




#pragma mark - UIImagePickerController图片代理
//选择照片完成之后的代理方法

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{

    //info是所选择照片的信息
    //UIImagePickerControllerEditedImage//编辑过的图片
    //UIImagePickerControllerOriginalImage//原图
    UIImage *resultImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    //使用模态返回到软件界面
    [self dismissViewControllerAnimated:YES completion:^{
        //返回后开始图片上传
        self.UpdataImg(resultImage);
    }];
    
}
@end
